﻿
var express=require("express");
var app=express();
var fs=require("fs");

var bodyParser=require("body-parser");
var multer=require('multer');

app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:false}));
app.use(multer({dest:'/tmp'}).array('image'));

app.get('/index2.html',function(req,resp){
	resp.sendFile(__dirname+"/"+"index2.html");
})

app.post('/file_upload',function(req,resp){
	console.log(req.files[0]); //上传文件信息
	
	var des_file=__dirname+"/"+req.files[0].originalname;
	
	resp.writeHead(200,{'Content-Type':'text/html;charset=utf8'});
	
	fs.readFile(req.files[0].path,function(err,data){
		fs.writeFile(des_file,data,function(err){
			if(err){
				console.log(err);
			}else{
				response={
					message:'File uploaded successfully',
					filename:req.files[0].originalname
				};
			}
			console.log(response);
			resp.end(JSON.stringify(response));
		})
	})
})

var server=app.listen(8082,function(){
	console.log("server running at 127.0.0.1:8082");
})